package sms

import (
	"encoding/json"
	"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
)

var client *dysmsapi.Client

func init() {
	var err error
	client, err = dysmsapi.NewClientWithAccessKey("cn-hangzhou", "LTAIfSUblQykqXRK", "EZGR7YwIh8CV4KcDiLYJvLLF7oabDW")
	if err != nil {
		panic(err)
	}
}

func Send(t Temp, mobile string, params interface{}) error {
	tc := t.Config()
	data, err := json.Marshal(params)
	if err != nil {
		return err
	}
	req := dysmsapi.CreateSendSmsRequest()
	req.Scheme = "https"
	req.PhoneNumbers = mobile
	req.SignName = tc.SignName
	req.TemplateCode = tc.TemplateCode
	req.TemplateParam = string(data)
	res, err := client.SendSms(req)
	if err != nil {
		return err
	}
	if res.Code != "OK" {
		return errors.Report2("发送失败", res)
	}
	return nil
}

type Temp int

type TempConfig struct {
	Name string
	SignName string
	TemplateCode string
}

const(
	TempNotice Temp = iota + 1
)

var TempMap = map[Temp]TempConfig{
	TempNotice:{
		"通知",
		"绿洲教育网",
		"SMS_200184165",
	},
}

func (t Temp) String() string {
	return TempMap[t].Name
}

func (t Temp) Config() TempConfig {
	return TempMap[t]
}
